import { UIIndex } from "../../cfw/tools/Define";
import UIManager from "../../cfw/ui/UIManager";
import SDKManager from "../SDKManager";
import BaseView from "../../cfw/mvc/BaseView";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName } from "../../public/Config";
import { BannerDir } from "../SDKConfig";
import BaseBanner from "../base/BaseBanner";
import BaseAd from "../base/BaseAd";

// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class SheduleBanner extends BaseView {

    protected count: number = 0;

    start() {
        if (SDKManager.getChannel().hasBanner()) {
            this.bannerUpdate()
            this.checkCount();
        }
        GlobalEvent.instance().on(EventName.BANNER_SHOW, this.bannerShow, this)
        GlobalEvent.instance().on(EventName.BANNER_HIDE, this.bannerHide, this)
    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.BANNER_SHOW, this.bannerShow, this)
        GlobalEvent.instance().off(EventName.BANNER_HIDE, this.bannerHide, this)
        if (SDKManager.getChannel().isSwtichOpen()) {
            SDKManager.getChannel().hideBanner()
        }
    }

    bannerHide() {
        console.log('bannerHide  ', this.count)
        this.count++;
    }

    bannerShow() {
        this.count--;
        if (this.count < 0) {
            this.count = 0;
        }
        console.log('bannerShow  ', this.count)
    }
    bannerUpdate() {

        // let matrix: MatrixMgr = SDKManager.getChannel().getMatrix();
        // if (matrix) {
        // let refreshTime = matrix.getZS_banner_break()
        let flag = SDKManager.getChannel().isSwtichOpen()
        console.log(' SheduleBanner bannerUpdate flag ', flag)
        if (flag) {

            this.setTimeout(() => {
                if (UIManager.instance().hasView('CrazyClickView', UIIndex.STACK)) {
                    this.bannerUpdate();
                    return;
                }
                if (SDKManager.getChannel().isSwtichOpen()) {
                    let flag = this.count == 0;
                    if (flag) {
                        let banner: BaseAd = SDKManager.getChannel().getBannerAd();
                        banner.setCallback(() => {
                            if (!cc.isValid(this.node)) {
                                return;
                            }
                            banner.show();
                            this.setTimeout(() => {
                                banner.hide();
                            }, 3000)

                        })
                        banner.load();
                    }


                    console.log(' bannerUpdate this.count ', this.count)
                    // let flag = this.count == 0;
                    // if (flag) {
                    //     SDKManager.getChannel().loadBanner(() => {
                    //         SDKManager.getChannel().bannerShow();
                    //         this.hideBanner();
                    //     });
                    // }

                    // if (flag) {

                    // }
                }


                // this.checkCount();
                this.bannerUpdate();
            }, 15 * 1000)
        }
        // }
    }

    checkCount() {
        if (this.count > 0) {
            return;
        }
        console.log(' SheduleBanner checkCount this.count  ', this.count)
        if (SDKManager.getChannel().isSwtichOpen()) {
            SDKManager.getChannel().showBanner(0, BannerDir.BOTTOM_MID)
            this.hideBanner();
        }


    }

    hideBanner() {
        this.setTimeout(() => {
            SDKManager.getChannel().hideBanner();
        }, 3000)
    }
    // update (dt) {}
}
